/**
 * BasePerference.java
 *
 * @author liujie11
 * 
 * @date 2014-3-19
 * 
 * Copyright 2014 Baidu. All rights reserved. 
 */
package com.baidu.common.preference;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class BasePerference {
    
    protected static SharedPreferences prefs;
    
    protected Context context = null;
    
    protected static BasePerference instance = new BasePerference();;
    
    public static BasePerference getInstance() {
        return instance;
    }
    
    public boolean isContextLive() {
        return instance.context != null;
    }
    
    @SuppressWarnings("static-access")
    public void setContext(Context context) {
        instance.context = context;
        instance.prefs = PreferenceManager.getDefaultSharedPreferences(context);
    }
    
    protected void checkPrefs() {
        if (prefs == null && context != null)
            prefs = PreferenceManager.getDefaultSharedPreferences(context);
    }
    
    public void save(String key, String value) {
        checkPrefs();
        if (prefs != null) {
            Editor editor = prefs.edit();
            editor.putString(key, value);
            editor.commit();
        }
    }
    
    public void save(String key, int value) {
        checkPrefs();
        if (prefs != null) {
            Editor editor = prefs.edit();
            editor.putInt(key, value);
            editor.commit();
        }
    }
    
    public void save(String key, boolean value) {
        checkPrefs();
        if (prefs != null) {
            Editor editor = prefs.edit();
            editor.putBoolean(key, value);
            editor.commit();
        }
    }
    
    public int getInt(String key) {
        checkPrefs();
        if (prefs != null)
            return prefs.getInt(key, 0);
        else
            return 0;
    }
    
    public int getInt(String key, int defaultValue) {
        checkPrefs();
        if (prefs != null)
            return prefs.getInt(key, defaultValue);
        else
            return defaultValue;
    }
    
    public String getString(String key) {
        checkPrefs();
        if (prefs != null)
            return prefs.getString(key, "");
        else
            return "";
    }
    
    public boolean getBoolean(String key) {
        checkPrefs();
        if (prefs != null)
            return prefs.getBoolean(key, false);
        else
            return false;
    }
    
    public boolean contains(String key){
        
        checkPrefs();
        if (prefs != null)
            return prefs.contains(key);
        else
            return false;
    }
    
    public boolean getBoolean(String key, boolean defaultValue) {
        checkPrefs();
        if (prefs != null)
            return prefs.getBoolean(key, defaultValue);
        else
            return false;
    }
    
    public void delete(String key) {
        checkPrefs();
        if (prefs != null) {
            Editor editor = prefs.edit();
            editor.remove(key);
            editor.commit();
        }
    }
    
}
